import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreMemberAddressService extends BaseService<any> {
  private readonly logger = new Logger(CoreMemberAddressService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getList
   * 对应 PHP: CoreMemberAddressService_core::getList()
   * 逻辑类型: undefined - undefined
   */
  async getList(where: any[]) {
    // 基于PHP真实逻辑: getList
    // PHP原文: if (empty($where['member_id'])) throw new CommonException('MEMBER_NOT_EXIST');         $field = 'id, member_id, name, mobile, province_id, city_id, di...
if (!where.member_id)) throw new BusinessException('MEMBER_NOT_EXIST'];
        const field = 'id, member_id, name, mobile, province_id, city_id, district_id, address, address_name, full_address, lng, lat, is_default';
        const order = 'is_default desc, id desc';

        const list = this.model.where([ ['site_id', '=', where.site_id],['member_id', '=', where.member_id ) )).field(field).order(order).select().toArray(];
        return list;
    }

    /**
     * 获取会员收货地址信息
     * @param int id
     * @param data
     * @return */
    async getInfo(int id, data)
    {
        if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,address_name,full_address,lng,lat,is_default';

        const info = this.model.field(field).where([ ['id', '=', id], ['site_id', '=', data.site_id], ['member_id', '=', data.member_id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加会员收货地址
     * @param data
     * @return mixed
     */
    async add(data)
    {
        if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        if (data.is_default) {
            this.model.where([ ['member_id', '=', data.member_id )  )).update([is_default: 0)];
  }

  /**
   * getInfo
   * 对应 PHP: CoreMemberAddressService_core::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: any[], data: any[]) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: if (empty($data['member_id'])) throw new CommonException('MEMBER_NOT_EXIST');         $field = 'id,member_id,name,mobile,province_id,city_id,district_...
if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,address_name,full_address,lng,lat,is_default';

        const info = this.model.field(field).where([ ['id', '=', id], ['site_id', '=', data.site_id], ['member_id', '=', data.member_id ) )).findOrEmpty().toArray(];
        return info;
    }

    /**
     * 添加会员收货地址
     * @param data
     * @return mixed
     */
    async add(data)
    {
        if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        if (data.is_default) {
            this.model.where([ ['member_id', '=', data.member_id )  )).update([is_default: 0)];
  }

  /**
   * add
   * 对应 PHP: CoreMemberAddressService_core::add()
   * 逻辑类型: undefined - undefined
   */
  async add(data: any[]) {
    // 基于PHP真实逻辑: add
    // PHP原文: if (empty($data['member_id'])) throw new CommonException('MEMBER_NOT_EXIST');         if ($data['is_default']) {             $this->model->where([ ['m...
if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        if (data.is_default) {
            this.model.where([ ['member_id', '=', data.member_id )  )).update([is_default: 0)];
  }

  /**
   * edit
   * 对应 PHP: CoreMemberAddressService_core::edit()
   * 逻辑类型: undefined - undefined
   */
  async edit(id: any[], data: any[]) {
    // 基于PHP真实逻辑: edit
    // PHP原文: if (empty($data['member_id'])) throw new CommonException('MEMBER_NOT_EXIST');         if ($data['is_default']) {             $this->model->where([ ['m...
if (!data.member_id)) throw new BusinessException('MEMBER_NOT_EXIST');
        if (data.is_default) {
            this.model.where([ ['member_id', '=', data.member_id ) )).update([is_default: 0)];
  }

  /**
   * getDefaultAddressByMemberId
   * 对应 PHP: CoreMemberAddressService_core::getDefaultAddressByMemberId()
   * 逻辑类型: undefined - undefined
   */
  async getDefaultAddressByMemberId(member_id: number) {
    // 基于PHP真实逻辑: getDefaultAddressByMemberId
    // PHP原文: $field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';         return $this->model->where([['mem...
const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['member_id', '=', member_id))).field(field).order('is_default desc').findOrEmpty().toArray(];
   }

    /**
     * 获取会员存在经纬度的地址
     * @param int member_id
     * @return */
   async getLngLatAddressByMemberId(int member_id){
        const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['member_id', '=', member_id], ['lng', '<>', ''], ['lat', '<>', ''))).field(field).order('is_default desc, id desc').findOrEmpty().toArray(];
   }

    /**
     * 获取收货地址
     * @param int id
     * @param int member_id
     * @return */
    async getMemberAddressById(int id, int member_id){
        const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['id', '=', id], ['member_id', '=', member_id))).field(field).findOrEmpty().toArray(;
  }
  }

  /**
   * getLngLatAddressByMemberId
   * 对应 PHP: CoreMemberAddressService_core::getLngLatAddressByMemberId()
   * 逻辑类型: undefined - undefined
   */
  async getLngLatAddressByMemberId(member_id: number) {
    // 基于PHP真实逻辑: getLngLatAddressByMemberId
    // PHP原文: $field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';         return $this->model->where([['mem...
const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['member_id', '=', member_id], ['lng', '<>', ''], ['lat', '<>', ''))).field(field).order('is_default desc, id desc').findOrEmpty().toArray(];
   }

    /**
     * 获取收货地址
     * @param int id
     * @param int member_id
     * @return */
    async getMemberAddressById(int id, int member_id){
        const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['id', '=', id], ['member_id', '=', member_id))).field(field).findOrEmpty().toArray(;
  }
  }

  /**
   * getMemberAddressById
   * 对应 PHP: CoreMemberAddressService_core::getMemberAddressById()
   * 逻辑类型: undefined - undefined
   */
  async getMemberAddressById(id: number, member_id: number) {
    // 基于PHP真实逻辑: getMemberAddressById
    // PHP原文: $field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';         return $this->model->where([['id'...
const field = 'id,member_id,name,mobile,province_id,city_id,district_id,address,full_address,lng,lat,is_default';
        return this.model.where([['id', '=', id], ['member_id', '=', member_id))).field(field).findOrEmpty().toArray(;
  }
  }
}
